﻿using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;

namespace GiveAQuiz.Web.Helpers
{
  public static class MessageHelper
  {
    public const string INFO = "msg-info";
    public const string WARNING = "msg-warning";
    public const string ERROR = "msg-error";
    public const string FLASH_INFO = "flash-info";

    /// <summary>
    /// Add a message to the information message section.
    /// </summary>
    /// <param name="controller">A Controller to extend.</param>
    /// <param name="message">The message to display.</param>
    public static void AddInfoMessage(this Controller controller, string message)
    {
      AddMessage(controller.TempData, INFO, message);
    }

    /// <summary>
    /// Add a message to the warning message section.
    /// </summary>
    /// <param name="controller">A Controller to extend.</param>
    /// <param name="message">The message to display.</param>
    public static void AddWarningMessage(this Controller controller, string message)
    {
      AddMessage(controller.TempData, WARNING, message);
    }

    /// <summary>
    /// Add a message to the error message section.
    /// </summary>
    /// <param name="controller">A Controller to extend.</param>
    /// <param name="message">The message to display.</param>
    public static void AddErrorMessage(this Controller controller, string message)
    {
      AddMessage(controller.TempData, ERROR, message);
    }

    /// <summary>
    /// Add a message that will be flashed at the top of a screen.
    /// </summary>
    /// <param name="controller">A Controller to extend.</param>
    /// <param name="message">The message to flash.</param>
    public static void FlashMessage(this Controller controller, string message)
    {
      controller.TempData[FLASH_INFO] = message;
    }

    /// <summary>
    /// Register the validation messages that are found in the 
    /// ModelState to the error message section.
    /// </summary>
    /// <param name="html"></param>
    public static void RegisterValidationMessages(this HtmlHelper html)
    {
      if ( !html.ViewContext.ViewData.ModelState.IsValid )
      {
        // add the ModelState error messages to TempData to get picked up
        // by the MessageSection.ascx view
        html.ViewContext.ViewData.ModelState.Values
          .SelectMany(val => val.Errors)
          .Each(error => AddMessage(html.ViewContext.TempData, ERROR, error.ErrorMessage));
      }
    }

    /// <summary>
    /// Add a message to the List of messages for that particular key.
    /// </summary>
    /// <param name="tempData">The TempDataDictionary.</param>
    /// <param name="key">The message key.</param>
    /// <param name="message">The message to display.</param>
    private static void AddMessage(TempDataDictionary tempData, string key, string message)
    {
      IList<string> msgs = tempData[key] as IList<string> ?? new List<string>();
      msgs.Add(message);
      tempData[key] = msgs;
    }

  }
}